package com.mashrabenaab;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Log;

public class GlobalService {
	private static SharedPreferences appSettings;

	public static void HandleException(Exception e) {
		Log.e("Class name is ", e.getStackTrace()[1].getClassName());
		Log.e("Method name is ", e.getStackTrace()[1].getMethodName());
		// e.printStackTrace();
	}

	public static boolean isOnline(Context cxt) {
		ConnectivityManager connectivityManager = (ConnectivityManager) cxt
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager
				.getActiveNetworkInfo();
		return activeNetworkInfo != null;

	}

	public static boolean isHostAvailable(String hostUrl) {
		Socket socket = null;
		boolean reachable = false;
		try {
			try {
				socket = new Socket(hostUrl, 80);
				reachable = true;
			} catch (UnknownHostException e) {
				GlobalService.HandleException(e);
				// reachable = false;
			} catch (IOException e) {
				GlobalService.HandleException(e);
				// reachable = false;
			}
		} finally {
			if (socket != null)
				try {
					socket.close();
				} catch (IOException e) {
					GlobalService.HandleException(e);
				}
		}
		return reachable;
	}

	public static boolean isHostAvailable1(String hostUrl) {
		try {
			InetAddress.getByName(hostUrl).isReachable(5000);
			// InetAddress.getByAddress(hostUrl, null).isReachable(5000);
			return true;
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static String[] splitString(String original, String separator) {
		Vector nodes = new Vector();
		int index = original.indexOf(separator);
		while (index >= 0) {
			nodes.addElement(original.substring(0, index));
			original = original.substring(index + separator.length());
			index = original.indexOf(separator);
		}
		nodes.addElement(original);
		String[] result = new String[nodes.size()];
		if (nodes.size() > 0) {
			for (int loop = 0; loop < nodes.size(); loop++) {
				result[loop] = (String) nodes.elementAt(loop);
			}
		}
		return result;
	}

	public static void MessageBox(String message, Context ctx) {
		AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);
		alertbox.setMessage(message);

		alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
			}
		});
		alertbox.show();
	}

	public static String getMonthName(String monthInNumber) {
		if (monthInNumber.equalsIgnoreCase("1")) {
			return "January";
		} else if (monthInNumber.equalsIgnoreCase("2")) {
			return "February";
		} else if (monthInNumber.equalsIgnoreCase("3")) {
			return "March";
		} else if (monthInNumber.equalsIgnoreCase("4")) {
			return "April";
		} else if (monthInNumber.equalsIgnoreCase("5")) {
			return "May";
		} else if (monthInNumber.equalsIgnoreCase("6")) {
			return "June";
		} else if (monthInNumber.equalsIgnoreCase("7")) {
			return "July";
		} else if (monthInNumber.equalsIgnoreCase("8")) {
			return "August";
		} else if (monthInNumber.equalsIgnoreCase("9")) {
			return "September";
		} else if (monthInNumber.equalsIgnoreCase("10")) {
			return "October";
		} else if (monthInNumber.equalsIgnoreCase("11")) {
			return "November";
		} else if (monthInNumber.equalsIgnoreCase("12")) {
			return "December";
		}
		return "";
	}

	public static String getFolderNameLocal(Magazine magazine) {
		String path = "mashrabenaab/" + magazine.getMonthName() + "-"
				+ magazine.getYearName();
		return path;
	}

	public static String getFolderNameLive(Magazine magazine, Context ctx) {
		String path = ctx.getString(R.string.url_base)
				+ magazine.getMonthName() + "-" + magazine.getYearName() + "/";
		return path;
	}

	public static boolean createLocalDirIfNotExists(Magazine magazine) {
		boolean ret = true;

		File dir = new File(Environment.getExternalStorageDirectory(),
				getFolderNameLocal(magazine));
		if (!dir.exists()) {
			if (!dir.mkdirs()) {
				Log.e("Mashrab e naab :: ", "Problem creating Image folder");
				ret = false;
			}
		}

		

		/*
		 * try { File file = new File(Environment.getExternalStorageDirectory()
		 * + "/mashrabenaab/", "info.txt"); if (!file.exists()) { if
		 * (!file.createNewFile()) { Log.e("Mashrab e naab :: ",
		 * "Problem creating info file"); } else { Log.e("Mashrab e naab :: ",
		 * "File created successfully "); } } else { Log.e("Mashrab e naab :: ",
		 * "File already exist"); } } catch (Exception e) {
		 * Log.e("Mashrab e naab Exception :: ", e.toString()); }
		 */

		/*
		 * try { FileOutputStream f = new FileOutputStream(file);
		 * 
		 * 
		 * } catch (FileNotFoundException e) { // TODO Auto-generated catch
		 * block e.printStackTrace(); }
		 */

		return ret;
	}

}
